/**
 * CityService.java 
 * Copyright © 2015-3015, 杭州泊享网络科技有限公司
 * 
 * @author Liu Tao
 * @create 2013年8月14日
 */
package com.pshare.artemis.basic.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pshare.artemis.basic.bean.City;
import com.pshare.artemis.basic.mapper.CityMapper;
import com.pshare.framework.mapper.BaseMapper;
import com.pshare.framework.service.BaseService;
import com.pshare.tool.StaticMapStore;

@Service
@Transactional
public class CityService extends BaseService<Integer, City> {

	private static final Logger logger = Logger.getLogger(CityService.class);
	
	@Autowired
	private CityMapper cityMapper;

	@Override
	protected BaseMapper<Integer, City> getBaseMapper() {
		return cityMapper;
	}

	@SuppressWarnings("unchecked")
	public List<City> queryAll() {
		boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.CITY);
		if (bExist) {
			Object rs = StaticMapStore.getInstance().get(StaticMapStore.Key.CITY);
			return (List<City>) rs;
		}
		return cityMapper.queryAll();
	}

	public String getCityName(Integer cityId) {
		if (cityId == null) {
    		return null;
    	}
		boolean bExist = StaticMapStore.getInstance().containsKey(StaticMapStore.Key.CITYMAP);
		if (bExist) {
			Map<Integer, String> rsMap = (Map<Integer, String>) StaticMapStore.getInstance().get(StaticMapStore.Key.CITYMAP);
			return rsMap.get(cityId) == null ? "" : rsMap.get(cityId);
		}
		return "";
	}
	
	/**
	 * 根据省份ID读取城市ID
	 * @param provinceId
	 * @return List<City>
	 */
	public List<City> getCitiesByProvinceId(Integer provinceId) {
		if (provinceId == null) {
			return new ArrayList<City>();
		}
		return cityMapper.queryByProvinceId(provinceId);
	}
	
	public String getCityCode(Integer cityId) {
		if (cityId == null) {
			return "";
		}
		City city = cityMapper.get(cityId);
		return city == null ? "" : city.getCode();
	}
	
	/**
	 * 根据市ID读取其所在省份ID
	 * @param cityId
	 * @return Integer
	 */
	public Integer getProvinceIdByCityId(Integer cityId) {
		if (cityId == null) {
			return null;
		}
		return cityMapper.getProvinceIdByCityId(cityId);
	}
	
    public City get(Integer id) {
    	return cityMapper.get(id);
    }
	
	public Integer findIdByName(String name) {
		if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
    	return cityMapper.findIdByName(name);
	}
	
	public City findByName(String name) {
		if (StringUtils.isBlank(name)) {
    		logger.warn("name is blank");
    		return null;
    	}
    	return cityMapper.findByName(name);
	}
	
	/**
	 * 根据城市名完全匹配查找城市ID
	 * @param name
	 * @return Integer
	 */
	public Integer getIdByName(String name) {
		if (StringUtils.isBlank(name)) {
			return null;
		}
		return cityMapper.getIdByName(name);
	}

}
